import time,random
import sthz as gb
from dm import dm
import logging 

logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                    level=logging.DEBUG,
                    filename='test.log',
                    filemode='a')
hwnd = dm.FindWindow("","pc01") 
dm_ret = dm.BindWindowEx(hwnd,'dx2',"normal","normal","normal",0)
dm.MoveWindow(hwnd,0,0)
yx_start_pos = None
while True:
    # 765 566 游戏相对位置
    yx_start_pos = dm.FindPic(0,0,1024,768,'./nyz_dt/init_pos.bmp',"000000",0.9,3)
    print(yx_start_pos)
    if yx_start_pos[0]!=-1:
        yx_start_pos = (yx_start_pos[1]-765,yx_start_pos[2]-566)
        break
    else:
        print("获取游戏初始位置失败")
print(yx_start_pos)

def move_click(x,y,yx_start_pos=0):
    gb.movemouseto(x,y)
    time.sleep(random.uniform(0.3,0.8))
    gb.pressandreleasemousebutton(1)
def move_click_with_xd(x,y,yx_start_pos):
    gb.movemouseto(x+yx_start_pos[0],y+yx_start_pos[1])
    dm.Capture(x+yx_start_pos[0], y+yx_start_pos[1], x+yx_start_pos[0]+100, y+yx_start_pos[1]+200, f'./gw/{random.random()}.bmp')
    time.sleep(random.uniform(0.3,0.8))
    gb.pressandreleasemousebutton(1)
def check_has_pl(dm):
    dm_ret = dm.SetDict(0,'./word/晶体小数字.txt')
    i = 0
    while True:
        pl = dm.Ocr(702+yx_start_pos[0],589+yx_start_pos[1],750+yx_start_pos[0],601+yx_start_pos[1],"c8c8c8-123456",1.0)
        print(pl)
        dm_ret = dm.SetDict(0,'./word/怪物字库.txt')
        pl = pl.split('/')
        i+=1
        if i>=3:
            return False
        if pl[0]=='':
            i+=1
        elif int(pl[0])<=0:
            return False
        return True